//
//  NearByTableViewController.swift
//  DriverSchoolAPP
//
//  Created by Logel on 2017/5/18.
//  Copyright © 2017年 Logel. All rights reserved.
//

import UIKit

class NearByTableViewController: UITableViewController,LgCallBack {
    var pList = [NearBy]()
    var destSelect = 0
    override func viewDidLoad() {
        super.viewDidLoad()

        getData()
    }
    
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return pList.count
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "reuseCell", for: indexPath) as! NearByTableViewCell
        cell.nb = pList[indexPath.row]
        // Configure the cell...

        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        destSelect = indexPath.row
        self.performSegue(withIdentifier: "toStudentsList", sender: nil)
        
    }
    
    
    
    func getData(){
        asyn_req_post(method:M_PlacesByAddress, cb: self, p: ["longitude":APP_lon as AnyObject,"latitude":APP_lat as AnyObject])
    }
    
    func callBack(method: String, data: AnyObject) {
        pList.removeAll()
        let json = JSON(data)
        print(json)
        pList = parseNearByList(all: json["MobileBody"].arrayValue)
        self.tableView.reloadData()
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        switch segue.identifier! {
        case "toStudentsList":
            let dest = segue.destination as! MyEnrollViewController
            dest.placeId = pList[destSelect].id
            dest.placeN = pList[destSelect].name
        default:
            break
        }
    }

    
}
